<?php

namespace Apps\AppApi\Method\User;


use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\MobileSMS;
use Apps\Common\Models\UserBase;

class BindMobile extends BaseMethod
{
    /**
     * @var int
     */
    private $success = 0;

    public function run()
    {
        if(false == $this->verifyUserAuth()){
            return false;
        }

        $mobile = $this->getDataItem('mobile', '');//帐号
        $smsCode = $this->getDataItem('sms_code','');//短信验证

        if($this->userInfoByAccount($mobile)){
            $this->setResult([
                'success' => $this->success,
                'message' => '手机号码已经使用过了.'
            ]);
            return false;
        }
        if(!MobileSMS::Check('update_mobile', $smsCode, $mobile, $this->apiAccessToken->id)) {
            $this->setResult([
                'success' => $this->success,
                'message' => '短信验证码错误'
            ]);
            return false;
        }

        // 修改用户手机号码
        if(UserBase::SetMobile($this->getUserAuth()->userId, $mobile)) {
            $this->setResult([
                'success' => 1,
                'message' => '绑定手机号码成功.'
            ]);
        } else {
            $this->setResult([
                'success' => 0,
                'message' => '保存数据异常.'
            ]);
        }
    }


}